$(function () {
  if (!localStorage.getItem("token")) {
    layui.layer.msg("登录失败，token缺失", { icon: 5 }, function () {
      localStorage.removeItem("token");
      // 2. 强制跳转到登录页面
      window.parent.location.href = "/bigevent-end/login.html";
    });
  }

  var p = {
    pagenum: 1,
    pagesize: 10,
  };
  initlist();

  function initlist() {
    $.ajax({
      url: "/api/articles/all",
      method: "get",
      data: p,
      success: function (res) {
        if (res.status !== 0) {
          return layui.layer.msg(res.message, { icon: 5 });
        }
        var html = template("tpl", res);
        $("tbody").html(html);
        page(res.total);
      },
    });
  }

  function initstate(state) {
    $.ajax({
      url: "/api/articles/" + state,
      method: "get",
      data: p,
      success: function (res) {
        if (res.status !== 0) {
          return layui.layer.msg(res.message, { icon: 5 });
        }
        var html = template("tpl", res);
        $("tbody").html(html);
        page(res.total);
      },
    });
  }
  var state;
  $("#form-search").on("submit", function (e) {
    e.preventDefault();
    state = $("[name=state]").val();
    if (!state) {
      initlist();
    } else {
      initstate(state);
    }
  });

  function page(a) {
    layui.laypage.render({
      elem: "pageBox",
      count: a,
      limit: p.pagesize,
      limits: [2, 4, 6, 8, 10],
      curr: p.pagenum,
      layout: ["limit", "page", "prev", "next", "count", "skip"],
      jump: function (obj, first) {
        if (first) {
          return;
          // do something
        }
        p.pagesize = obj.limit;
        var current = obj.curr;
        p.pagenum = current;
        if (!state) {
          initlist();
        } else {
          initstate(state);
        }
      },
    });
  }

  $("tbody").on("click", ".btn-delete", function () {
    var len = $(".btn-delete").length;
    var id = $(this).attr("data-id");
    layui.layer.confirm("是否删除？", { icon: 3, title: "提示" }, function (index) {
      $.ajax({
        url: "/api/articles/" + id,
        method: "delete",
        success: function (res) {
          if (res.status !== 0) {
            return layui.layer.msg(res.message, { icon: 5 });
          }
          layui.layer.msg("删除文章成功", { icon: 6 });
          if (len === 1 && p.pagenum > 1) {
            p.pagenum--;
          }
          if (!state) {
            initlist();
          } else {
            initstate(state);
          }
        },
      });
      layer.close(index);
    });
  });

  $("tbody").on("click", "span", function () {
    let id = $(this).attr("data-id");
    let status = $(this).attr("data-status");
    $.ajax({
      url: "/api/articles/" + id,
      method: "post",
      data: {
        state: status,
      },
      success: function (res) {
        if (res.status !== 0) {
          return layui.layer.msg(res.message, { icon: 5 });
        }
        layui.layer.msg('"切换状态成功"', { icon: 6 });
        console.log(state);
        if (!state) {
          initlist();
        } else {
          initstate(state);
        }
      },
    });
  });

  $("tbody").on("click", ".btn-edit", function () {
    var id = $(this).attr("data-id");
    location.href = "/bigevent-end/article/art_pub.html?" + id;
  });
});
